package imoocvideodev.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import imoocvideodev.common.result.IMoocJSONResult;
import imoocvideodev.common.result.PagedResult;
import imoocvideodev.common.util.TimeAgoUtils;
import imoocvideodev.common.util.UUIDUtils;
import imoocvideodev.dao.*;
import imoocvideodev.pojo.*;
import imoocvideodev.service.VideoService;
import imoocvideodev.vo.CommentVO;
import imoocvideodev.vo.VideosVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author: Redi
 * @Date: 2021/2/6 21:47
 * @Description: 视频服务实现类
 */
@Service
public class VideoServiceImpl implements VideoService {

    @Autowired
    VideosMapper videosMapper;
    @Autowired
    VideosMapperCustom videosMapperCustom;
    @Autowired
    SearchRecordsMapper searchRecordsMapper;
    @Autowired
    UsersLikeVideosMapper usersLikeVideosMapper;
    @Autowired
    UsersMapper usersMapper;
    @Autowired
    CommentsMapper commentsMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public IMoocJSONResult update(Videos videos){
        int resultCount = videosMapper.updateByPrimaryKeySelective(videos);
        if (resultCount > 0){
            return IMoocJSONResult.okMsg("视频信息更新成功！");
        }
        return IMoocJSONResult.errorMsg("视频信息更新失败!");
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public String insert(Videos videos){
        videosMapper.insertSelective(videos);
        return videos.getId();
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public PagedResult findAllVideos(Videos video, Integer isSaveRecord,Integer page, Integer pageSize){
        //保存热搜词
        String desc = video.getVideoDesc();
        if (isSaveRecord != null && isSaveRecord == 1){
            SearchRecords record = new SearchRecords();
            String searchId = UUIDUtils.getRandomUUID();
            record.setId(searchId);
            record.setContent(desc);
            searchRecordsMapper.insert(record);
        }

        List<VideosVO> lists = videosMapperCustom.findAllVideos(desc);
        PageHelper.startPage(page,pageSize);
        PageInfo pageInfo = new PageInfo<>(lists);
        PagedResult pagedResult = new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageInfo.getPages());
        pagedResult.setRows(lists);
        pagedResult.setRecords(pageInfo.getTotal());
        return pagedResult;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<String> getHotWords() {
        return searchRecordsMapper.selectHotWords();
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public IMoocJSONResult userLikeVideo(String userId, String videoId, String videoCreaterId) {
        UsersLikeVideos record = new UsersLikeVideos();
        record.setId(UUIDUtils.getRandomUUID());
        record.setUserId(userId);
        record.setVideoId(videoId);
        try{
            //插入用户点赞数据
            usersLikeVideosMapper.insert(record);
            //视频点赞数++
            videosMapper.addVideoLikeCount(videoId);
            //用户被点赞数目++
            usersMapper.addUserReceivedLikeCount(videoCreaterId);
            return IMoocJSONResult.okMsg("用户点赞成功");
        }catch (Exception e){
            return IMoocJSONResult.errorMsg("用户点赞失败");
        }
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public IMoocJSONResult userUnLikeVideo(String userId, String videoId, String videoCreaterId) {
        UsersLikeVideos record = usersLikeVideosMapper.selectByUserIdAndVideoId(userId, videoId);
        try{
            //删除用户点赞数据
            usersLikeVideosMapper.deleteByPrimaryKey(record.getId());
            //视频点赞数--
            videosMapper.reduceVideoLikeCount(videoId);
            //用户被点赞数目++
            usersMapper.reduceUserReceivedLikeCount(videoCreaterId);
            return IMoocJSONResult.okMsg("用户取消点赞成功");
        }catch (Exception e){
            return IMoocJSONResult.errorMsg("用户取消点赞失败");
        }
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public IMoocJSONResult saveComment(Comments comments) {
        comments.setId(UUIDUtils.getRandomUUID());
        comments.setCreateTime(new Date());
        int resultCount = commentsMapper.insert(comments);
        if (resultCount > 0){
            return IMoocJSONResult.okData(comments);
        }
        return IMoocJSONResult.errorMsg("保存评论失败...");
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PagedResult getAllComments(String videoId, Integer page, Integer size) {
        List<CommentVO> voList = commentsMapper.selectAllCommentsByVideoId(videoId);

        for(CommentVO commentsItem: voList){
            String timeAgo = TimeAgoUtils.format(commentsItem.getCreateTime());
            commentsItem.setTimeAgoStr(timeAgo);
        }
        PageHelper.startPage(page,size);
        PageInfo<CommentVO> pageList = new PageInfo<>(voList);
        PagedResult pagedResult = new PagedResult();
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(voList);
        pagedResult.setPage(page);
        pagedResult.setRecords(pageList.getTotal());

        return pagedResult;
    }
}
